package com.gopay.cashier.web.utils;

import java.math.BigDecimal;

import javax.annotation.Resource;

import com.gopay.cashier.service.security.GopayDtCertService;
import com.gopay.common.security.vo.UserDtCertInfoVO;
import org.springframework.stereotype.Service;

import com.gopay.cashier.service.CertFreeQuotaService;
import com.gopay.common.domain.cust.CustCertFreeQuota;
import com.gopay.common.user.manager.UserDcPwdManager;

/**
 * @author gaocl
 * @2016年7月12日 下午3:02:58 www.gopay.com.cn Inc.All rights reserved.
 */
@Service("certFreeQuotaHelper")
public class CertFreeQuotaHelper {
    
    @Resource(name="certFreeQuotaService")
    private CertFreeQuotaService certFreeQuotaService;
    
    @Resource(name = "userDcPwdManager")
    private UserDcPwdManager userDcPwdManager;

    @Resource(name = "gopayDtCertService")
    private GopayDtCertService gopayDtCertService;
    
    public boolean checkFreeQuota(BigDecimal tranAmt,String custId,String userId){
        boolean flag=true;
        CustCertFreeQuota custCertFreeQuota = certFreeQuotaService.getByCustId(custId);
        if(null!=custCertFreeQuota){
            if("01".equals(custCertFreeQuota.getStat())
                    &&"02".equals(custCertFreeQuota.getCertType())){ 
                
                int compareTo = tranAmt.compareTo(custCertFreeQuota.getQuota());
                if((compareTo==-1)||(compareTo == 0)){
                    flag=false;  
                  } 
                
            }else if("01".equals(custCertFreeQuota.getStat())
                    &&"01".equals(custCertFreeQuota.getCertType())){
                BigDecimal limitAmt = custCertFreeQuota.getQuota();
                if(limitAmt.compareTo(tranAmt)>=0){
                    flag = true;//免验通过，支付密码支付即可
                }else{
                    flag = false;//免验不通过，支付密码、数字证书支付
                }
                
            }else if("02".equals(custCertFreeQuota.getStat()) 
                    &&"01".equals(custCertFreeQuota.getCertType())){
                flag = false;// 免验状态为关闭，使用数字证书+支付密码支付
            }
        }else{ //如果没设置免验
//            boolean dcAuth = userDcPwdManager.getDCAuth(custId);
            UserDtCertInfoVO userDtCertInfoVO = gopayDtCertService.getOnOffAndCertSN(userId);

            if(userDtCertInfoVO.isDtSwitch()){     //若动态口令开启  则需要显示动态口令输入框
                flag = true; 
            }else{
                flag = false;//查询不到免验记录，不免验   用户数字证书
            }
        }
        return  flag;
    }

}
